set more off

*** merge variables ***

use "turnout/turnout", clear
merge 1:1 fips using "vap/vap"
drop _m
merge 1:1 fips using "distance/distance"
drop _m
merge 1:1 fips using "uninsured/uninsured"
drop _m
merge 1:1 fips using "demographics/demographics"
drop _m
merge 1:1 fips using "demvoteshare/demvoteshare"
drop _m
merge m:1 fipsstate using "gis/states"
drop STATENS AFFGEOID GEOID STUSPS LSAD ALAND AWATER
drop _m
merge m:1 fipsstate using "expansion/expansion"
drop _m
merge m:1 fipsstate using "electionsheld/electionsheld"
drop _m
merge m:1 fipsstate using "eligibility/eligibility"
drop _m
merge m:1 fips using "gis/counties"
drop _m
merge m:1 fipsstate using "migration/migration"
drop _m
merge m:1 stateabbr using "exchanges/exchanges"
drop _m

*** make new variables ***

forvalues year = 2004(2)2016 {
	
	* turnout
	gen turnout`year' = totalvoting`year'/vap`year'*100
	replace turnout`year' = . if turnout`year' == 0 | round(turnout`year') == 0
	if `year' > 2004 {
		local year_2 = `year'-2
		gen dturnout`year'`year_2' = turnout`year' - turnout`year_2'
		su dturnout`year'`year_2', det
		replace dturnout`year'`year_2' = . if dturnout`year'`year_2' > r(p99) | dturnout`year'`year_2' < r(p1)
	}
	if `year' > 2006 {
		local year_4 = `year'-4
		gen dturnout`year'`year_4' = turnout`year' - turnout`year_4'
		su dturnout`year'`year_4', det
		replace dturnout`year'`year_4' = . if dturnout`year'`year_4' > r(p99) | dturnout`year'`year_4' < r(p1)
	}
	if `year' > 2008 {
		local year_6 = `year'-6
		gen dturnout`year'`year_6' = turnout`year' - turnout`year_6'
		su dturnout`year'`year_6', det
		replace dturnout`year'`year_6' = . if dturnout`year'`year_6' > r(p99) | dturnout`year'`year_6' < r(p1)
	}
	if `year' > 2010 {
		local year_8 = `year'-8
		gen dturnout`year'`year_8' = turnout`year' - turnout`year_8'
		su dturnout`year'`year_8', det
		replace dturnout`year'`year_8' = . if dturnout`year'`year_8' > r(p99) | dturnout`year'`year_8' < r(p1)
	}
	if `year' > 2012 {
		local year_10 = `year'-10
		gen dturnout`year'`year_10' = turnout`year' - turnout`year_10'
		su dturnout`year'`year_10', det
		replace dturnout`year'`year_10' = . if dturnout`year'`year_10' > r(p99) | dturnout`year'`year_10' < r(p1)
	}
	if `year' > 2014 {
		local year_12 = `year'-12
		gen dturnout`year'`year_12' = turnout`year' - turnout`year_12'
		su dturnout`year'`year_12', det
		replace dturnout`year'`year_12' = . if dturnout`year'`year_12' > r(p99) | dturnout`year'`year_12' < r(p1)
	}
	
	* registration
	gen registration`year' = totalregistered`year'/vap`year'*100
	replace registration`year' = . if registration`year' == 0
	if `year' > 2004 {
		local year_2 = `year'-2
		gen dregistration`year'`year_2' = registration`year' - registration`year_2'
		su dregistration`year'`year_2', det
		replace dregistration`year'`year_2' = . if dregistration`year'`year_2' > r(p99) | dregistration`year'`year_2' < r(p1)
	}
	if `year' > 2006 {
		local year_4 = `year'-4
		gen dregistration`year'`year_4' = registration`year' - registration`year_4'
		su dregistration`year'`year_4', det
		replace dregistration`year'`year_4' = . if dregistration`year'`year_4' > r(p99) | dregistration`year'`year_4' < r(p1)
	}
	if `year' > 2008 {
		local year_6 = `year'-6
		gen dregistration`year'`year_6' = registration`year' - registration`year_6'
		su dregistration`year'`year_6', det
		replace dregistration`year'`year_6' = . if dregistration`year'`year_6' > r(p99) | dregistration`year'`year_6' < r(p1)
	}
	if `year' > 2010 {
		local year_8 = `year'-8
		gen dregistration`year'`year_8' = registration`year' - registration`year_8'
		su dregistration`year'`year_8', det
		replace dregistration`year'`year_8' = . if dregistration`year'`year_8' > r(p99) | dregistration`year'`year_8' < r(p1)
	}	
	if `year' > 2012 {
		local year_10 = `year'-10
		gen dregistration`year'`year_10' = registration`year' - registration`year_10'
		su dregistration`year'`year_10', det
		replace dregistration`year'`year_10' = . if dregistration`year'`year_10' > r(p99) | dregistration`year'`year_10' < r(p1)
	}
	if `year' > 2014 {
		local year_12 = `year'-12
		gen dregistration`year'`year_12' = registration`year' - registration`year_12'
		su dregistration`year'`year_12', det
		replace dregistration`year'`year_12' = . if dregistration`year'`year_12' > r(p99) | dregistration`year'`year_12' < r(p1)
	}
}

forvalues year = 2008(1)2015 {

	if `year' >= 2009 {
		local year_1 = `year'-1
		gen dpcti`year'`year_1' = pcti`year' - pcti`year_1'
		gen dpctui`year'`year_1' = pctui`year' - pctui`year_1'
	}
	if `year' >= 2010 {
		local year_2 = `year'-2
		gen dpcti`year'`year_2' = pcti`year' - pcti`year_2'
		gen dpctui`year'`year_2' = pctui`year' - pctui`year_2'
	}
	if `year' >= 2012 {
		local year_4 = `year'-4
		gen dpcti`year'`year_4' = pcti`year' - pcti`year_4'
		gen dpctui`year'`year_4' = pctui`year' - pctui`year_4'
	}

}
forvalues i = 2004(1)2016 {
	gen logvap`i' = log(vap`i' + 1)
	capture gen pcteligible`i' = nipr`i'/vapnosenior`i'
}

*** elections held ***

gen Gub2014Sen2014 = Gub2014 * Sen2014 
gen Gub2010Sen2010 = Gub2010 * Sen2010

*** only use county once (minimum distance) ***

gen x = .
gen border = ""
forvalues i = 1(1)6 {
	replace x = distance`i' if ismainsample`i' == 1 & mi(x) 
	replace border = border`i' if ismainsample`i' == 1 & mi(border)
}

*** merge in 2-d distance ***

drop if mi(fips)
merge 1:1 fips border using "gis/borderswithtwodimensionaldistance"
keep if _m == 3
gen latXlon = latit*long
gen lat2 = latit^2
gen lon2 = long^2

*** treatment ***

gen z = x > 0
gen zx = z*x

*** drop unused variables ***

#delimit;

local myvars 
	fips fipsstate stateabbr z x zx
	pcti* pctui*
	turnout*
	registration*
	dpcti* dpctui*
	dturnout*
	dregistration*
	pctpov pctwhite pct65plus pcths lmedianincome vap* logvap* demvoteshare2012
	swing2012 Gub2010 Sen2010 Gub2010Sen2010 Gub2014 Sen2014 Gub2014Sen2014
	border
	jan12
	pcteligible*
	latitude longitude latXlon border_* lat2 lon2
	netmig* inmig* outmig*
	fedmarketplace
	_id
	;
	
#delimit cr

keep `myvars'
order `myvars'

*** impose consistent sample ***

drop if mi(z) | mi(x)
forvalues i = 2008(1)2015 {
	drop if mi(pcti`i')
}
forvalues i = 2004(2)2016 {
	drop if mi(turnout`i')
}

*** only has one state ***

drop if border == "OK-NM"

*** hide restricted data ***

foreach x in turnout2004 turnout2006 turnout2008 turnout2010 turnout2012 turnout2014 turnout2016 registration2004 registration2006 registration2008 registration2010 registration2012 registration2014 registration2016 {
	replace `x' = runiform()*100
}
foreach x in dturnout20062004 dturnout20082006 dturnout20082004 dturnout20102008 dturnout20102006 dturnout20102004 dturnout20122010 dturnout20122008 dturnout20122006 dturnout20122004 dturnout20142012 dturnout20142010 dturnout20142008 dturnout20142006 dturnout20142004 dturnout20162014 dturnout20162012 dturnout20162010 dturnout20162008 dturnout20162006 dturnout20162004 dregistration20062004 dregistration20082006 dregistration20082004 dregistration20102008 dregistration20102006 dregistration20102004 dregistration20122010 dregistration20122008 dregistration20122006 dregistration20122004 dregistration20142012 dregistration20142010 dregistration20142008 dregistration20142006 dregistration20142004 dregistration20162014 dregistration20162012 dregistration20162010 dregistration20162008 dregistration20162006 dregistration20162004 {
	replace `x' = rnormal()*50
}

*** save ***

drop if mi(fips)
save dataset, replace
